package cn.agiledata.bank.webService.ser;

/*
 * Copyright (c) 2003-2005 Lan-in corporation.  All rights reserved.
 *
 */
 
//import here

import org.apache.axis.utils.Messages;
import org.apache.axis.encoding.ser.SimpleDeserializer;

import javax.xml.namespace.QName;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  DateTime Deserializer, deserialize xsd:dateTime type data to
 *  java.util.Date type object, this deserializer ignore TimeZone
 *  information, just convert year, month, day, hour, minute, second
 *  and millisecond, millisecond is optional.
 *  
 */
public class DateTimeDeserializer extends SimpleDeserializer
{

    private static SimpleDateFormat fmt =
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

    /**
     * The Deserializer is constructed with the xmlType and
     * javaType
     */
    public DateTimeDeserializer(Class javaType, QName xmlType)
    {
        super(javaType, xmlType);
    }

    /**
     * The simple deserializer provides most of the stuff.
     * We just need to override makeValue().
     * convert dateTime string to java.util.Date object
     */
    public Object makeValue(String source)
    {
        Date date = null;

        // validate fixed portion of format
        if ( source != null ) {
            if (source.length() < 19)
                throw new NumberFormatException(
                           Messages.getMessage("badDateTime00"));

            if (source.charAt(4) != '-' || source.charAt(7) != '-' ||
                source.charAt(10) != 'T')
                throw new NumberFormatException(
                                                Messages.getMessage("badDate00"));

            if (source.charAt(13) != ':' || source.charAt(16) != ':')
                throw new NumberFormatException(
                                                Messages.getMessage("badTime00"));
        }

        // convert what we have validated so far
        try
        {
            synchronized (fmt)
            {
                date = fmt.parse(source == null ? null :
                                    (source.substring(0,19)+".000") );
            }
        }
        catch(Exception e)
        {
            throw new NumberFormatException(e.toString());
        }

        int pos = 19;

        // parse optional milliseconds
        if ( source != null )
        {
            if (pos < source.length() && source.charAt(pos)=='.')
            {
                int milliseconds = 0;
                int start = ++pos;
                while (pos<source.length() && Character.isDigit(source.charAt(pos)))
                    pos++;

                String decimal = source.substring(start, pos);
                if (decimal.length() == 3)
                {
                    milliseconds = Integer.parseInt(decimal);
                } else if (decimal.length() < 3)
                {
                    milliseconds=Integer.parseInt((decimal+"000").substring(0, 3));
                } else
                {
                    milliseconds=Integer.parseInt(decimal.substring(0, 3));
                    if (decimal.charAt(3) >= '5')
                        ++milliseconds;
                }

                // add milliseconds to the current date
                date.setTime(date.getTime() + milliseconds);
            }
        }
        
        return date;
    }
}